/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.cslg.dorm.web;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.cslg.dorm.entity.DormBuilding;
import com.thinkgem.jeesite.cslg.dorm.entity.DormRoom;
import com.thinkgem.jeesite.cslg.dorm.service.DormBuildingService;
import com.thinkgem.jeesite.cslg.dorm.service.DormRoomService;

/**
 * 宿舍表Controller
 * @author 张文锦
 * @version 2017-01-08
 */
@Controller
@RequestMapping(value = "${adminPath}/dorm/dormRoom")
public class DormRoomController extends BaseController {

	@Autowired
	private DormRoomService dormRoomService;
	
	@Autowired
	private DormBuildingService  dormBuildingService;
	
	@ModelAttribute
	public DormRoom get(@RequestParam(required=false) String id) {
		DormRoom entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = dormRoomService.get(id);
		}
		if (entity == null){
			entity = new DormRoom();
		}
		return entity;
	}
	
	@RequiresPermissions("dorm:dormRoom:view")
	@RequestMapping(value = {"list", ""})
	public String list(DormRoom dormRoom, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<DormRoom> page = dormRoomService.findPage(new Page<DormRoom>(request, response), dormRoom); 
		model.addAttribute("page", page);
		return "cslg/dorm/dormRoomList";
	}

	@RequiresPermissions("dorm:dormRoom:view")
	@RequestMapping(value = "form")
	public String form(DormRoom dormRoom, Model model) {
		model.addAttribute("dormRoom", dormRoom);
		List<DormBuilding> dormBuildings=dormBuildingService.findList(new DormBuilding());
		model.addAttribute("dormBuildings",dormBuildings);
		return "cslg/dorm/dormRoomForm";
	}

	@RequiresPermissions("dorm:dormRoom:edit")
	@RequestMapping(value = "save")
	public String save(DormRoom dormRoom, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, dormRoom)){
			return form(dormRoom, model);
		}
		dormRoomService.save(dormRoom);
		addMessage(redirectAttributes, "保存宿舍成功");
		return "redirect:"+Global.getAdminPath()+"/dorm/dormRoom/?repage";
	}
	
	@RequiresPermissions("dorm:dormRoom:edit")
	@RequestMapping(value = "delete")
	public String delete(DormRoom dormRoom, RedirectAttributes redirectAttributes) {
		dormRoomService.delete(dormRoom);
		addMessage(redirectAttributes, "删除宿舍成功");
		return "redirect:"+Global.getAdminPath()+"/dorm/dormRoom/?repage";
	}

}